﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITAP.ShprossesBuilder.Modelling.Components;
using System.Drawing;
using ITAP.ShprossesBuilder.Modelling;
using ITAP.Core.Modelling;

namespace ITAP.ShprossesBuilder.Manipulation
{
    public class CommandDiscreteRotateShprossConnector : SerializedUndoRedo, IModelComponentCommand
    {
        #region IModelComponentCommand Members
        private List<IComponent> componentsForProcessing;
        private double tolerance;

        public CommandDiscreteRotateShprossConnector(ModelControl modelControl, double tolerance)
        {
            this.modelControl = modelControl;
            this.tolerance = tolerance;
        }

        public List<ITAP.ShprossesBuilder.Modelling.Components.IComponent> ComponentsForProcessing
        {
            get
            {
                return componentsForProcessing;
            }
            set
            {
                componentsForProcessing = value;
            }
        }

        #endregion

        #region IModelControlCommand Members

        private MouseManagerEvent mouseEvent;
        public MouseManagerEvent MouseEvent
        {
            get
            {
                return mouseEvent;
            }
            set
            {
                mouseEvent = value;
            }
        }


        public ModelControl ModelControl
        {
            get
            {
                return modelControl;
            }
            set
            {
                modelControl = value;
            }
        }

        private Point mouseDownPoint;

        public System.Drawing.Point MouseDownPoint
        {
            get
            {
                return mouseDownPoint;
            }
            set
            {
                mouseDownPoint = value;
            }
        }

        private Point mouseUpPoint;
        public System.Drawing.Point MouseUpPoint
        {
            get
            {
                return mouseUpPoint;
            }
            set
            {
                mouseUpPoint = value;
            }
        }

        private Point mouseMovePoint;
        public System.Drawing.Point MouseMovePoint
        {
            get
            {
                return mouseMovePoint;
            }
            set
            {
                mouseMovePoint = value;
            }
        }

        public bool ExecuteImmediately
        {
            get
            {
                return true;
            }
            set
            {
            }
        }

        #endregion

        #region ICommand Members

        public void StartExecute()
        {
            componentsForProcessing.Clear();
            foreach (IComponent selectedComponent in modelControl.ModelContainer.SelectedComponentsStack)
            {
                if (selectedComponent is ShprossConnector)
                    this.componentsForProcessing.Add(selectedComponent);
            }
            this.prevMovePoint = this.mouseDownPoint;
        }

        private Point prevMovePoint;
        public void Execute()
        {
            Point offsetPoint = new Point(this.mouseMovePoint.X - prevMovePoint.X, this.mouseMovePoint.Y - this.prevMovePoint.Y);
            ModelPoint modelOffsetPoint = modelControl.ScreenToModel(offsetPoint);
            if (Math.Abs(modelOffsetPoint.Y) < tolerance)
                return;

            foreach(ShprossConnector connector in componentsForProcessing)
            {
                if (Math.Sign(modelOffsetPoint.Y) > 0)
                    connector.Rotate(Math.PI / 4);
                else
                    connector.Rotate(-Math.PI / 4);
            }
            
        }

        public void EndExecute()
        {            
        }


        public bool IsNeedModelUpdate
        {
            get { return true; }
        }

        public string Name
        {
            get { return "Дискретное вращение соединителей"; }
        }

        private bool enabled;
        public bool Enabled
        {
            get
            {
                return enabled;
            }
            set
            {
                enabled = value;
            }
        }

        #endregion

        #region ICloneable Members

        public object Clone()
        {
            CommandDiscreteRotateShprossConnector clone = new CommandDiscreteRotateShprossConnector(this.modelControl, tolerance);
            clone.enabled = this.enabled;
            clone.componentsForProcessing = new List<IComponent>(this.componentsForProcessing);
            clone.modelControl = this.modelControl;

            if (this.undoModelContainer != null)
                clone.undoModelContainer = (ModelContainer)this.undoModelContainer.Clone();
            if (this.redoModelContainer != null)
                clone.redoModelContainer = (ModelContainer)this.redoModelContainer.Clone();

            return clone;

        }

        #endregion

        #region ICommand Members


        public bool IsNeedVisitorsUpdate
        {
            get { return true; }
        }

        #endregion
    }
}
